package com.solrtest.controller;

import com.solrtest.doman.Order;
import com.solrtest.doman.Product;
import com.solrtest.doman.Shop;
import com.solrtest.service.BatchSync;
import com.solrtest.service.ProductService;
import com.solrtest.service.TemplateProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
public class IndexController {

    private final ProductService productService;
    private final TemplateProductService templateProductService;
    private final BatchSync batchSync;

    @Autowired
    public IndexController(ProductService productService, TemplateProductService templateProductService, BatchSync batchSync) {
        this.productService = productService;
        this.templateProductService = templateProductService;
        this.batchSync = batchSync;
    }


    //=====================================页面

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/course")
    public String course() {
        return "course";
    }

    @GetMapping("/solrclient")
    public String solrClient() {
        return "solrclient";
    }

    @GetMapping("/solrtemplate")
    public String solrtemplate() {
        return "solrtemplate";
    }

    @GetMapping("/batchsync")
    public String batchsync() {
        return "batchsync";
    }

    //=====================================SolrClient
    @PostMapping("/add")
    @ResponseBody
    public Object add(Product product) {
        productService.add(product);
        return "新增成功!";
    }

    @PostMapping("/edit")
    @ResponseBody
    public Object edit(Product product) {
        productService.edit(product);
        return "修改成功!";
    }

    @PostMapping("/del")
    @ResponseBody
    public Object del(String id) {
        productService.del(id);
        return "删除成功!";
    }

    @PostMapping("/queryAll")
    @ResponseBody
    public List<Product> queryAll() {
        return productService.queryAll();
    }

    @PostMapping("/query")
    @ResponseBody
    public List<Product> query(Product product) {
        return productService.query(product);
    }


    //=====================================SolrTemplate
    @PostMapping("/addTemp")
    @ResponseBody
    public Object addTemp(Product product) {
        templateProductService.add(product);
        return "新增成功!";
    }

    @PostMapping("/editTemp")
    @ResponseBody
    public Object editTemp(Product product) {
        templateProductService.edit(product);
        return "修改成功!";
    }

    @PostMapping("/delTemp")
    @ResponseBody
    public Object delTemp(String id) {
        templateProductService.del(id);
        return "删除成功!";
    }

    @PostMapping("/queryByIdsTemp")
    @ResponseBody
    public List<Product> queryByIdsTemp(String ids) {
        return templateProductService.queryByIds(Arrays.asList(ids.split(",")));
    }

    @PostMapping("/queryAllTemp")
    @ResponseBody
    public Page<Product> queryAllTemp() {
        return templateProductService.queryAll();
    }

    @PostMapping("/queryTemp")
    @ResponseBody
    public List<Product> queryTemp(Product product) {
        return templateProductService.query(product);
    }


    //=====================================BatchSync
    @PostMapping("/readUseTimeOrder")
    @ResponseBody
    public Object readUseTimeOrder(int act) {
        return batchSync.readUseTimeOrder(act);
    }

    @PostMapping("/batchSyncOrder")
    @ResponseBody
    public Object batchSyncOrder(int act) {
        return batchSync.BatchSyncOrder(act);
    }


    @PostMapping("/queryOrder")
    @ResponseBody
    public Object queryOrder(Order order) {
        Page<Order> page = batchSync.queryOrder(order);
        LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("总条数", page.getTotalElements());
        resultMap.put("总页数", page.getTotalPages());
        resultMap.put("数据", page.getContent());
        return resultMap;
    }


    //=====================================经纬度
    @PostMapping("/addShopInfo")
    @ResponseBody
    public Object addShopInfo() {
        templateProductService.addShopInfo();
        return "同步门店信息成功！";
    }

    @PostMapping("/getByGps")
    @ResponseBody
    public List<Shop> getByGps(String gps) {
        return productService.getByGps(gps);
    }

    @PostMapping("/readTime")
    @ResponseBody
    public Object readTime() {
        return templateProductService.readTime();
    }
}
